Ask a Question
Send an email to me.
 
Java Question and Answer Forums
JavaRanch Big Moose Saloon
Marcus Green's Discussion Forum
java.sun.com Forums, Chat and User Groups
Artima Java Answers Forum
 
Other Resources
Java Language Specification
Java Virtual Machine Specification
Java 2 Platform, Standard Edition, v 1.4.0 API Specification
 
Tutorials
Learning the Java Language
Operator Precedence Chart, Expressions, Statements, Blocks
Programming with Assertions
 
Answers: Certified Java Programmer Mock Exam
No.AnswerRemark
1a  Prints: main,B.m1  The no parameter version of B.m1 overrides A.m1. Therefore, B.m1 must comply with the rules of method overriding. The single parameter version of B.m1 overloads the m1 method and therefore must comply with the rules of method overloading. For example, one version of the overloaded method may be an instance method while the other is a static method.  
2c  Compile-time error.  Fields declared in an interface are implicitly public, final, and static. A compile-time error is generated in response to the attempt to increment the value of i.  
3c  f  final  public  All field declarations within an interface are implicitly public, static and final. Use of these modifiers is redundant but legal. Although "const" is a Java keyword it is not currently used by the Java programming language. An interface member can never be private or protected.  
4c  Compile-time error.  In the declarations for interface B the keyword extends has been replaced by the keyword implements.  
5b  Prints: ABC  In all three cases, the object passed to method m1 is an instance of class C; however, the type of the reference is different for each method. Since member variables are accessed based on the type of the reference, the value printed by each method is different even though the same object instance is used for each method invocation.  
6c  Prints: F.printS1 E.printS2  An instance method can override a superclass method, but a static method does not override a superclass method. Instead, a static method can hide a superclass method. The static method that is accessed will depend on the type of the reference.  
7c  Prints: CBA  The method, m, is static; therefore, the method that is actually invoked at runtime is based on the type of the reference and not based on the type of the object instance at runtime. When the object reference is of type A, then the method from A is invoked even though the type of the object instance at runtime is C.  
8a  Prints: AAA  Class A has only one implementation of method m1. Class B overloads method m1 with a second implementation. Class C overloads method m1 with a third implementation. Reference c2 refers to an object of type A so only the A.m1 implementation is accessible.  
9i  Compiles and runs without error.  Although the reference named base is of type Base, the object that it refers to is of type Sub. Therefore, the object may be cast to type Sub. Since objects of type Sub implement both interfaces, I1 and I2, the Sub type objects can be assigned to references of type I1 and I2 without an explicit cast.  
10b  Prints: String  Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. End of quote. In this case, the String type is more specific than the Object type because String extends Object.  
11d  Prints: BB  Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. End of quote. In this case, B is more specific than A because B extends A. The method m(B b, B b) is more specific than the others because both parameters are the most specific.  
12 All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface the usage is redundant and is discouraged. Since all methods declared within an interface are implicitly public a weaker access level can not be declared for an implementing method. The final, synchronized, native, and strictfp modifiers can not appear in the declaration of an abstract method but they can be added to an implementation of an abstract method.  
13b  Prints: ABC  One of the overloaded methods is selected based on the type of the argument. The type of the argument is determined by the reference type. The type of c1 is A. The type of c2 is B. The type of c3 is C.  
14a  b  c  d  A Cat object inherits Fur and four Legs from the Dog super class.  A Cat object is able to sleep and eat.  A Cat object is able to climb a tree.  The relationship between Dog and Pet is an example of an appropriate use of inheritance.  An appropriate inheritance relationship includes a sub class that "is-a" special kind of the super class. The relationship between the Dog subclass and the Pet superclass is an example of an appropriate inheritance relationship because a Dog "is-a" Pet. The relationship between the Cat sub class and the Dog superclass is not an example of an appropriate use of inheritance because a Cat is not a Dog.  
15a  Prints: float,float  An argument of type char can be widened to an int, long, float or double. An argument of type long can be widened to a float or a double. Since the float type is the most specific type available here it selected before double.  
16g  Compiler error at line 10.  The reference "base" is of type "Base[]", therefore an explicit cast to type "Base[]" is required.  
17c  Compiler error.  Section 15.12.2.2 of the Java Language Specification states the following. If more than one method declaration is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error. End of quote. In this case, both the String class and class A are more specific than type Object because both extend Object. However, String and A are equally specific because one does not extend the other. The ambiguity generates a compiler error.  

Copyright © 2002, Dan Chisholm
All rights reserved.
1